package net.woggle.shackbrowseapi;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.*;

import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.parser.Parser;
import org.jsoup.select.Elements;

import com.google.appengine.labs.repackaged.org.json.JSONArray;
import com.google.appengine.labs.repackaged.org.json.JSONException;
import com.google.appengine.labs.repackaged.org.json.JSONObject;


@SuppressWarnings("serial")
public class Rootpost extends HttpServlet {
	
	static final String LOGIN_URL = "https://www.shacknews.com/account/signin";
	static final String PAGE_URL = "http://www.shacknews.com/chatty";
	static final String USER_AGENT = "shackbrowseAPI/1.0";
	
	class RootPost
	{
		String body;
		String category;
		String postId;
		String author;
		String date;
		int depth = 0;
	}
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		
		resp.setContentType("application/json; charset=UTF-8");
		
		String id = req.getParameter("id");
		
		if (id == null)
			return;
		
		Document doc = Jsoup.parse(LoginDownload.get("http://www.shacknews.com/frame_chatty.x?root=" + id));
		
		Element rootpost = doc.getElementById("item_" + id);
		
		RootPost reply = new RootPost();
		reply.body = rootpost.getElementsByClass("postbody").get(0).html();
		reply.date = rootpost.getElementsByClass("postdate").get(0).ownText();
		
		
		Set<String> names = rootpost.getElementsByClass("fullpost").get(0).classNames();
		for (String name : names)
		{
			if (name.contains("olmod_"))
				reply.category = name.substring(6);
			if (name.contains("fpmod_"))
				reply.category = name.substring(6);
		}
	
		reply.author = rootpost.getElementsByClass("user").get(0).child(0).ownText();
		reply.depth = 0;
		reply.postId = id;
    			
		JSONObject result = new JSONObject();
		
		try {
			result.put("id", reply.postId);
			result.put("author", reply.author);
			result.put("date", reply.date);
			result.put("body", reply.body);
			result.put("category", reply.category);
			result.put("depth", reply.depth);
		} catch (JSONException f) {
			// TODO Auto-generated catch block
			f.printStackTrace();
		}
    		
		JSONArray comments = new JSONArray();
		comments.put(result);
    			
    	
		JSONObject container = new JSONObject();
		try {
			container.put("replies", comments);
		} catch (JSONException g) {
			// TODO Auto-generated catch block
			g.printStackTrace();
		}
		resp.getWriter().print(container.toString());
	}
}
